package validadores;

import entidades.Processo;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import rn.ProcessoRN;
/**
 *
 * @author Marcelo Gomes Neto
 */
public class ValidaPam implements Validator
{
    public ValidaPam(){}
    
    @Override
    public void validate(FacesContext context, UIComponent toValidate, Object value) throws ValidatorException 
    {
        String pam = (String) value;
        ProcessoRN processoRN = new ProcessoRN();
        Processo processo = processoRN.buscarPorPam(pam);
        
        try
        {
            if (pam.equalsIgnoreCase(processo.getPam())) 
            {
                ((UIInput)toValidate).setValid(true);
                FacesMessage message = new FacesMessage("Pam já existe!");
                context.addMessage(toValidate.getClientId(context), message);
            }
        }
        catch (Exception e) {  
            //e.printStackTrace(); 
            //System.out.println("O ERRO É: "+e.getMessage());
        }
    }
}